<?php

namespace App\Http\Controllers\Api;

use App\Models\Banner;
use App\Models\Config;
use App\Models\Goods;
use App\Models\HotGoods;
use App\Models\Version;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{
    public function banner_lists()
    {

        $banner = Banner::orderBy('sort','desc')->get(['img','type','banner_id']);
        foreach ($banner as $value){
            $value->my_img;
        }

        return ['status_code'=>1,'data'=>$banner];
    }

    public function banner_detail(Request $request )
    {
        $banner = Banner::find($request->get('banner_id'),['detail']);
        return ['status_code'=>1,'data'=>$banner];
    }

    //热门推荐
    public function hot_goods()
    {
        $goodses = HotGoods::where('type',1)->orderBy('sort','desc')->get();
        $data=[];
        foreach ($goodses as $goods){
            $find_goods =  $goods->goods;
            $find_goods->cover = $find_goods->my_picture()->where('type',1)->first()->my_pic_path;
            $data[] = $find_goods;
        }
        return ['status_code'=>1,'data'=>$data];
    }
    //猜你喜欢
    public function like_goods()
    {
        $goodses = HotGoods::where('type',2)->orderBy('sort','desc')->get();
        $data=[];
        foreach ($goodses as $goods){
            $find_goods =  $goods->goods;
            $find_goods->cover = $find_goods->my_picture()->where('type',1)->first()->my_pic_path;
            $data[] = $find_goods;
        }
        return ['status_code'=>1,'data'=>$data];
    }
    public function search(Request $request)
    {
        $goodses = Goods::where('name','like','%'.$request->get('keywords').'%')->paginate(20);
        foreach ($goodses as $value){
            $value->setHidden(['detail']);
            $cover = $value->my_picture()->where('type',1)->first();
            $value->cover = $cover->my_pic_path;
        }
        return ['status_code'=>1,'data'=>$goodses];
    }

    public function aboutus()
    {
        $about = Config::where('name','ABOUTUS')->first();
        return ['status_code','data'=>$about->value];
    }

    public function interule()
    {
        $about = Config::where('name','INTERULE')->first();
        return ['status_code','data'=>$about->value];
    }

    public function version()
    {
        $app = Version::orderBy('created_at','desc')->first();
        return ['data'=>$app];
    }


}
